﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AALib.Input;

namespace AALib.Screen
{
    public class ScreenManager : DrawableGameComponent
    {
        public Dictionary<string, Screen> m_screens = new Dictionary<string, Screen>();
        SpriteBatch m_spritebatch = null;

        InputManager m_inputManager = null;
        public Screen m_currentScreen = null;

        // le temp

        #region Properties
        public SpriteBatch Spritebatch
        {
            get { return m_spritebatch; }
        }

        public InputManager InputManager
        {
            get { return m_inputManager; }
        }

        public string CurrentScreen
        {
            //get { return currentScreen; }
            set
            {
                if (m_currentScreen != null)
                {
                    m_currentScreen = m_screens[m_currentScreen.GetNextScreen()];
                    if (m_currentScreen == null)
                    {
                        Game.Exit();
                    }
                    else
                    {
                        m_currentScreen.Initialize();
                    }
                }
                else
                {
                    m_screens.TryGetValue(value, out m_currentScreen);
                    if (m_currentScreen == null)
                    {
                        Game.Exit();
                    }
                    else
                    {
                        m_currentScreen.Initialize();
                    }
                }
            }
        }
        #endregion

        public ScreenManager(Game p_game)
            : base(p_game)
        { }

        public override void Initialize()
        {
            m_inputManager = new InputManager(Game);
            m_inputManager.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            m_spritebatch = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            m_inputManager.Update(gameTime);

            if (m_currentScreen != null)
            {
                m_currentScreen.HandleInput(ref m_inputManager);
                m_currentScreen.Update(gameTime);
                if (m_currentScreen.m_changeState)
                {
                    Screen nextState = null;
                    m_screens.TryGetValue(m_currentScreen.GetNextScreen(), out nextState);
                    if (nextState != null)
                    {
                        m_currentScreen.Shutdown();
                        m_currentScreen = nextState;
                        m_currentScreen.Initialize();
                    }
                    else
                    {
                        Game.Exit();
                    }
                }
            }

            // base.Update(gameTime);
        }

        public override void Draw(GameTime p_gameTime)
        {
            //Spritebatch.Begin();

            if (m_currentScreen != null)
            {
                m_currentScreen.Draw(p_gameTime);
            }
            base.Draw(p_gameTime);

            //Spritebatch.End();
        }

        public void AddScreen(string p_id, Screen p_screen)
        {
            m_screens.Add(p_id, p_screen);
        }

        public void RemoveScreen(string p_id)
        {
            Screen screen = m_screens[p_id];
            if (screen != null)
            {
                screen.Dispose();
                m_screens.Remove(p_id);
            }
        }
    }
}
